package com.hp.service.impl;

import com.hp.context.Config;
import com.hp.dao.PowerDao;
import com.hp.entity.Power;
import com.hp.entity.PowerTreeVo;
import com.hp.entity.RolePowerVo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.PowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Transactional
@Service
public class PowerServiceImpl implements PowerService {
    @Autowired
    private PowerDao powerDao;

    @Transactional(readOnly = true)
    @Override
    public List<Power> findAllByRoleId(int id) {
        return powerDao.findAllByRoleId(id);
    }
    @Transactional(readOnly = true)
    @Override
    public List<PowerTreeVo> findAllByParent(List<Power> list) {
        List powerlist = new ArrayList();
        List<Power> allByParent = powerDao.findAllByParent();     //对应菜单父类
        for (Power power : allByParent) {
            PowerTreeVo powerTreeVo = new PowerTreeVo(power, list);
            powerlist.add(powerTreeVo);
        }
        return powerlist;
    }

    @Override
    public SubmitResultInfo authPowerByRole(List<RolePowerVo> rolePowerVos) {

        powerDao.deleteAllRolePowerByRid(rolePowerVos.get(0).getRid());//删除tbl_role_power中的该角色数据
        powerDao.addRolePower(rolePowerVos);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 666, new Object[]{"角色功能权限"}));

    }
}
